package com.sqsf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqsf.javabean.Permission;
import com.sqsf.javabean.Role;
import com.sqsf.javabean.RoleList;
import com.sqsf.service.PermissionService;
import com.sqsf.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.RequestWrapper;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit){
        PageHelper.startPage(page, limit);
        List<Role> all = roleService.findAll();
        PageInfo<Role> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);

        return "/role/role-list";
    }

    //删除角色
    @RequestMapping("/delete")
    public String delete(int id){
        roleService.delete(id);
        return "redirect:/role/findAll";
    }

    //批量删除
    @RequestMapping("/deleteIds")
    @ResponseBody
    public String deleteIds(@RequestBody List<Integer> ids){
        System.out.println(ids);
        for (Integer id : ids) {
            roleService.delete(id);
        }

        return "/role/findAll";
    }


    //角色添加页面跳转
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "/role/role-add";
    }
    //角色添加
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Role role){

        roleService.add(role);
        return "redirect:/role/findAll";
    }

    //角色详情
    @RequestMapping("/show")
    public String show(HttpServletRequest request,int id){
        Role role = roleService.findRole_Permission(id);
        request.setAttribute("role",role);
        return "/role/role-show";
    }

    //跳转角色修改页面
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(HttpServletRequest request,int id){
        Role role = roleService.findRole_Permission(id);
        request.setAttribute("role",role);
        return "/role/role-edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Role role){
        roleService.edit(role);
        return "redirect:/role/findAll";
    }

    //跳转角色添加权限页面
    @RequestMapping(value = "/permissionAdd",method = RequestMethod.GET)
    public String permissionAdd(HttpServletRequest request,int id){
        //查询所有权限信息
        List<Permission> all = permissionService.findAll();
        request.setAttribute("permissionList",all);
        //根据id查询数据
        Role role = roleService.findRole_Permission(id);
        request.setAttribute("permissionIdList",role.getPermissions());
        //返回角色id
        request.setAttribute("roleId",id);
        return "/role/role-edit-permission";
    }

    //权限添加
    @RequestMapping(value = "/permissionAdd",method = RequestMethod.POST)
    @ResponseBody
    public String permissionAdd(@RequestBody RoleList roleList){
        roleService.editPermission(roleList.getRid(),roleList.getIds());
        return "/role/findAll";
    }
}
